﻿using RemoteData.model;
using System;
using System.Linq;
using System.Windows.Data;
using System.Windows.Media;

namespace RemoteData.common.converter
{
    public class cellBackgroundConvert : IMultiValueConverter
    {
        public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (values == null || values.Length != 4)
            {
                throw new ArgumentException("values count doesn't match");
            }

            UpdateStatus[] statusList = values[0] as UpdateStatus[];

            string[] colors = ((string)values[1]).Split(',');
            if (colors.Count() != 3)
            {
                throw new ArgumentException("parameters count doesn't match");
            }
            string warnBackColor = colors[0]; //"#8B008B"
            string colorIndex0 = colors[1]; //#f5f5f7
            string colorIndex1 = colors[2]; //Colors.AliceBlue(FFF0F8FF)

            int columnNo = (int)values[2];
            int channelIndex = columnNo - 1;//减去前面一个通道号列

            if (channelIndex < statusList.Length && channelIndex >= 0)
            {
                int val = statusList[channelIndex].status;
                if (statusList[channelIndex].updateFlag)
                {
                    if (statusList[channelIndex].status == 1)
                    {
                        return new SolidColorBrush((Color)ColorConverter.ConvertFromString(warnBackColor));
                    }
                    else
                    {
                        int alternationIndex = (int)values[3];
                        if (alternationIndex == 0)
                        {
                            return new SolidColorBrush((Color)ColorConverter.ConvertFromString(colorIndex0));
                        }
                        else
                        {
                            //return new SolidColorBrush(Colors.AliceBlue);
                            return new SolidColorBrush((Color)ColorConverter.ConvertFromString(colorIndex1));
                        }
                    }
                }

            }

            return null;
        }

        public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotSupportedException();
        }
    }
}
